package com.qiyeji.real_estate.service;

import com.qiyeji.real_estate.entity.*;
import com.qiyeji.real_estate.mapper.projectFileInformationMapper;
import com.qiyeji.real_estate.mapper.projectMapper;
import com.qiyeji.real_estate.mapper.timeTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectFileInformationService {
    @Autowired
    projectFileInformationMapper projectFileInformationMapper;

    @Autowired
    projectMapper projectMapper;

    public int insert(projectFileInformation record){
        return projectFileInformationMapper.insert(record);
    }

    public int delete(projectFileInformationKey key){
        return projectFileInformationMapper.deleteByPrimaryKey(key);
    }

    public int updateByPrimaryKey(projectFileInformation record){
        return projectFileInformationMapper.updateByPrimaryKey(record);
    }

    public projectFileInformation selectByPrimaryKey(String key){
        return projectFileInformationMapper.selectByPrimaryKey(key);
    }

    public List<projectFileInformation> getAllProjectFileInformation(){
        return projectFileInformationMapper.getAllProjectFileInformation();
    }

    public List<projectFileInformation> fuzzyQuery(String key){
        return projectFileInformationMapper.fuzzyQuery(key);
    }

    public void mkdirFile(String fileName){
        IO io=new IO(fileName);
        String str = fileName.substring(0, fileName.indexOf("-"));
        project p=new project(projectMapper.getProjectByName(str).get(0));
        String pCode=p.getPcode();
        String path=System.getProperty("user.dir") + File.separator+"FileSystem"+File.separator+fileName;
        System.out.println(pCode);
        System.out.println(fileName);
        System.out.println(path);
        projectFileInformation record=new projectFileInformation(pCode,fileName,path);
        projectFileInformationMapper.insert(record);
    }

    public List<String> getAllFileName(){
        List<projectFileInformation> projectFileInformations=projectFileInformationMapper.getAllProjectFileInformation();
        List<String> files=new ArrayList<>();
        for(int i=0;i<projectFileInformations.size();i++) {
            File file = new File(projectFileInformations.get(i).getPath());
            File[] tempList = file.listFiles();
            for(int j=0;j<tempList.length;j++) {
                files.add(tempList[j].getName());
            }
        }
        return files;
    }

    public List<String> getUncreated(){
        List<String> list=new ArrayList<>();
        List<project> project=projectMapper.getAllProject();
        List<projectFileInformation> projectFileInformation=projectFileInformationMapper.getAllProjectFileInformation();
        for(int i=0;i<project.size();i++){
            int flag=0;
            String p=project.get(i).getPname();
            for(int j=0;j<projectFileInformation.size();j++){
                String pf=projectFileInformation.get(j).getFilename().split("-")[0];
                if(p.equals(pf)){
                    flag++;
                }
            }
            if(flag==0){
                list.add(p);
            }
        }
        return list;
    }

    public List<projectFileInformation> getFileNameBysCode(String sCode){
        return projectFileInformationMapper.getFileNameBysCode(sCode);
    }
    public int delete(String pCode){
        return projectFileInformationMapper.delete(pCode);
    }

}
